import { winNameMap } from "@/config";

export default function dispatchEventStroage() {
	const signSetItem = localStorage.setItem
	localStorage.setItem = function (key, val) {
		let setEvent = new Event('setItemEvent')
		setEvent.key = key
		setEvent.newValue = val
		window.dispatchEvent(setEvent)
		// eslint-disable-next-line prefer-rest-params
		signSetItem.apply(this, arguments)
	}
}

export function delay(time = 1000): Promise<boolean> {
  return new Promise(function (resolve) {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
}

function levenshteinDistance(a: string, b: string) {
  const dp = new Array(a.length + 1)
    .fill(null)
    .map(() => new Array(b.length + 1).fill(0));

  for (let i = 0; i <= a.length; i++) {
    dp[i][0] = i;
  }

  for (let j = 0; j <= b.length; j++) {
    dp[0][j] = j;
  }

  for (let i = 1; i <= a.length; i++) {
    for (let j = 1; j <= b.length; j++) {
      const cost = a[i - 1] !== b[j - 1] ? 1 : 0;
      dp[i][j] = Math.min(
        dp[i - 1][j] + 1,
        dp[i][j - 1] + 1,
        dp[i - 1][j - 1] + cost
      );
    }
  }

  return dp[a.length][b.length];
}
function stringSimilarity(a: string, b: string) {
  const distance = levenshteinDistance(a, b);
  const maxLength = Math.max(a.length, b.length);
  const similarity = 1 - distance / maxLength;
  return similarity;
}

export function getCurName(str: string) {
  let max = 0;
  let res = "";
  for (let index = 0; index < Object.keys(winNameMap).length; index++) {
    let key = Object.keys(winNameMap)[index];
    for (let i = 0; i < winNameMap[key].length; i++) {
      const name = winNameMap[key][i];
      if (str.includes(name) || name.includes(str)) return key;
      let value = stringSimilarity(str.toLocaleLowerCase(), name);
      if (value > max) {
        max = value;
        res = key;
      }
    }
  }
  if (max > 0.5) return res;
  return "";
}

// 
export function getSimilarName(strr: string, infoOptionList: any) {
  let max = 0;
  let res = '';
  let resS = '';
  const str = strr.toLocaleLowerCase();
  infoOptionList.map((item: any) => {
    const name = item.application_name;
    const nameS = item.application_name.toLocaleLowerCase();
    if (str.includes(nameS) || nameS.includes(str)) {
      res = name;
    }
    let value = stringSimilarity(str, nameS);
    if (value > max) {
      max = value;
      resS = name;
    }
  })
  if (res) return res;
  if (resS && max > 0.5) return resS;
  return "其他";
}
// 生成随机汉字
function getRandomChinese(): string {
  // 随机生成Unicode范围内的汉字
  const charCode = Math.floor(Math.random() * (40869 - 19968 + 1)) + 19968;
  return String.fromCharCode(charCode);
}

// 生成随机英文字母
function getRandomLetter(): string {
  // 随机生成ASCII码范围内的字母
  const charCode = Math.floor(Math.random() * (122 - 97 + 1)) + 97;
  return String.fromCharCode(charCode);
}

// 生成指定数量的随机汉字和英文字母
export function generateRandomChars(num: number): string {
  let randomChars = "";
  for (let i = 0; i < num; i++) {
    // 50%的概率生成汉字，50%的概率生成英文字母
    if (Math.random() < 0.5) {
      randomChars += getRandomChinese();
    } else {
      randomChars += getRandomLetter();
    }
  }
  return randomChars;
}

export function isElectron() {
  var userAgent = navigator.userAgent.toLowerCase();
  return userAgent.indexOf(" electron/") > -1;
}

export function windowMove(flag: boolean) {
  if (isElectron()) {
    const { ipcRenderer } = require('electron');
    ipcRenderer.send('window-move-open', flag);
  }
}

// 格式化日对象
export const getNowDate = () => {
  var date = new Date();
  var sign2 = "";
  var year = date.getFullYear() // 年
  var month = date.getMonth() + 1; // 月
  var day = date.getDate(); // 日
  var hour = date.getHours(); // 时
  var minutes = date.getMinutes(); // 分
  var seconds = date.getSeconds() //秒
  var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
  var week = weekArr[date.getDay()];
  // 给一位数的数据前面加 “0”
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  return year + "" + month + "" + day + "." + hour + sign2 + minutes + sign2 + seconds;
}

